package org.datastructure.firstday.tree;

import lombok.Data;
import lombok.ToString;

@ToString(exclude = {"left", "right"})
@Data
public class HeroNode {
    private int id;
    private String name;
    HeroNode left;
    HeroNode right;

    public HeroNode(int id, String name) {
        this.id = id;
        this.name = name;
    }


    /**
     * 删除策略：删除节点是叶子节点时直接删除
     * 删除节点非叶子时，且只有一个子节点时，提升到该节点位置
     * 删除节点为为子树时，将子节点提升到该节点位置
     * @param no
     */
    public void delStrategy(int no) {


    }

    public void delHero(int no) {
        if (this.left != null && this.left.id == no) {
            this.left = null;
            return;
        }
        if (this.right != null && this.right.id == no) {
            this.right = null;
            return;
        }
        if (this.left != null) {
            this.left.delHero(no);
        }
        if (this.right != null) {
            this.right.delHero(no);
        }
    }

    public void preOrder() {
        System.out.println(this);

        if (left != null) {
            this.left.preOrder();
        }
        if (right != null) {
            this.right.preOrder();
        }
    }

    public void infixOrder() {

        if (left != null) {
            this.left.infixOrder();
        }
        System.out.println(this);

        if (right != null) {
            this.right.infixOrder();
        }
    }

    public void postOrder() {
        if (left != null) {
            this.left.postOrder();
        }

        if (right != null) {
            this.right.postOrder();
        }
        System.out.println(this);
    }

    /**
     * 前序遍历查找
     *
     * @param no
     * @return
     */
    public HeroNode preOrderSearch(int no) {
        System.out.println("前序查找次数");
        if (this.id == no) {
            return this;
        }

        HeroNode res = null;
        if (left != null) {
            res = this.left.preOrderSearch(no);
        }
        if (res != null) {
            return res;
        }
        if (right != null) {
            res = this.right.preOrderSearch(no);
        }
        return res;
    }

    public HeroNode infixOrderSearch(int no) {
        HeroNode res = null;
        if (this.left != null) {
            res = this.left.infixOrderSearch(no);
        }
        if (res != null) {
            return res;
        }
        System.out.println("中序查找次数");
        if (this.id == no) {
            return this;
        }
        if (this.right != null) {
            res = this.right.infixOrderSearch(no);
        }
        return res;
    }

    public HeroNode postOrderSearch(int no) {
        HeroNode res = null;
        if (this.left != null) {
            res = this.left.postOrderSearch(no);
        }
        if (res != null) {
            return res;
        }
        if (this.right != null) {
            res = this.right.postOrderSearch(no);
        }
        if (res != null) {
            return res;
        }
        System.out.println("后序查找次数");
        if (this.id == no) {
            return this;
        }
        return res;
    }
}
